<template>
  <Table
    ref="tableRef"
    :url="URL_QUERYCOMPANYLIST"
    :columns="data.columns"
    :handles="data.handles"
    :toolbar="data.toolbar"
    :query="data.query"
    listKey="list"
  ></Table>
</template>

<script lang="ts" setup name="enterpriseList">
import Table from '@/components/table/index.vue';
import {
  URL_QUERYCOMPANYLIST,
  queryCompanyList,
  deleteCompany,
  frozenCompany,
} from '@/service/apis/enterprise';
import {
  CORPORATE_IDENTITY_TYPE,
  YES_NO,
  ATTR_TYPE,
} from '@/utils/dictionaries';
import { queryDicByParent } from '@/service/apis/api';

const router = useRouter();

const tableRef = ref();

const emit = defineEmits(['get-update']);
onActivated(() => {
  emit('get-update', (flag: boolean) => {
    if (flag) {
      tableRef.value?.getData();
    }
  });
});

const data = reactive({
  columns: [
    {
      prop: 'channelName',
      label: '平台运营商',
    },
    {
      prop: 'orgName',
      label: '企业名称',
    },
    {
      prop: 'type',
      label: '企业身份',
      dic: [] as any,
    },
    {
      prop: 'operatorName',
      label: '运营商名称',
    },
    {
      prop: 'attr',
      label: '运营商属性',
      dic: [] as any,
    },
    {
      prop: 'code',
      label: '统一社会信用代码',
    },
    {
      prop: 'contactUser',
      label: '联系人',
    },
    {
      prop: 'contactNum',
      label: '联系方式',
    },
    {
      prop: 'email',
      label: '邮箱',
    },
    {
      prop: 'isLimit',
      label: '是否限制电站',
      dic: [] as any,
    },
    {
      prop: 'isFrozen',
      label: '状态',
      template: (row: any, index: number) => {
        return row.isFrozen
          ? `<div style="color:#FA3939;">冻结</div>`
          : `<div style="color:#01C68B;">正常</div>`;
      },
    },
  ],
  handles: [
    {
      label: '详情',
      permission: 'enterpriseList.see',
      handle: (row: any) => {
        console.log('详情---', row);
        router.push({
          name: 'enterpriseDetail',
          query: row,
        });
      },
    },
    {
      label: '恢复',
      labelFun: (row: any) => {
        return row.isFrozen ? '恢复' : '冻结';
      },
      color: '#FA3939',
      permission: 'enterpriseList.frozen',
      handle: (row: any) => {
        const msg = row.isFrozen
          ? '是否确认执行本操作？'
          : '企业冻结后，该企业电站不再提供服务、用户无法登录移动端，是否确认执行本操作？';
        ElMessageBox.confirm(msg, '提示', {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          frozenCompany({
            id: row.id,
            isFrozen: !row.isFrozen,
          }).then((res: any) => {
            row.isFrozen = res.isFrozen;
          });
        });
      },
    },
    {
      label: '修改',
      permission: 'enterpriseList.edit',
      handle: (row: any) => {
        console.log('编辑---', row);
        router.push({
          name: 'enterpriseEdit',
          query: row,
        });
      },
    },
    {
      label: '删除',
      color: '#FA3939',
      permission: 'enterpriseList.delete',
      handle: (row: any) => {
        ElMessageBox.confirm(
          '删除企业会将该企业下的子企业一并删除，是否确定删除？',
          '提示',
          {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
          }
        ).then(() => {
          deleteCompany({
            id: row.id,
          }).then((res: any) => {
            tableRef.value.getData();
          });
        });
      },
    },
    {
      label: '企业钱包',
      color: '#01C68B',
      permission: 'enterpriseList.wallet',
      // disabledFun: (row: any) => {
      //   return row.type !== '1';
      // },
      hideFun: (row: any) => {
        return row.type !== '1';
      },
      handle: (row: any) => {
        // console.log('详情---', row);
        router.push({
          name: 'enterpriseWallet',
          query: { id: row.id, key: 'enterpriseList' },
        });
      },
    },
  ],
  toolbar: [
    {
      label: '新增',
      permission: 'enterpriseList.add',
      icon: 'icon-add',
      type: 'primary',
      handle: (row: any) => {
        const orgType = window.localStorage.getItem('orgType');
        if (orgType === '2' || orgType === '3') {
          ElMessage({
            showClose: true,
            message: '当前企业不支持创建子企业',
            type: 'warning',
          });
        } else {
          router.push({
            name: 'enterpriseAdd',
          });
        }
      },
    },
  ],
  query: [
    {
      label: '企业名称',
      prop: 'orgName',
      comName: 'input',
      props: {
        placeholder: '请输入企业名称',
      },
    },
    {
      label: '统一社会信用代码',
      prop: 'code',
      comName: 'input',
      props: {
        placeholder: '请输入统一社会信用代码',
      },
    },
    {
      label: '企业身份',
      prop: 'type',
      comName: 'select',
      props: {
        placeholder: '请选择企业身份',
        options: [] as any,
      },
    },
    {
      label: '联系人',
      prop: 'contactUser',
      comName: 'input',
      props: {
        placeholder: '请输入联系人',
      },
    },
    {
      label: '联系方式',
      prop: 'contactNum',
      comName: 'input',
      props: {
        placeholder: '请输入联系方式',
      },
    },
    {
      label: '平台运营商',
      prop: 'channelName',
      comName: 'input',
      props: {
        placeholder: '请输入平台运营商',
      },
    },
  ],
});
console.log('orgType====', window.localStorage.getItem('orgType'));

queryDicByParent({
  listDtos: ['corporateIdentity', 'stationLimitType', 'operatorProperties'],
}).then((res: any) => {
  data.columns[2].dic = [...res.corporateIdentity];
  data.columns[4].dic = [...res.operatorProperties];
  data.columns[9].dic = [...res.stationLimitType];
  data.query[2].props.options = [...res.corporateIdentity];
});
</script>